package com.sx.art2.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.Admin;
import com.sx.art2.beans.Art;
import com.sx.art2.beans.Category;
import com.sx.art2.beans.Order;
import com.sx.art2.beans.User;
import com.sx.art2.service.AdminService;
import com.sx.art2.service.ArtService;
import com.sx.art2.service.CategoryService;
import com.sx.art2.service.OrderService;
import com.sx.art2.service.StockService;
import com.sx.art2.service.SupplierService;
import com.sx.art2.service.UserService;

@Controller
@SessionAttributes(types={Admin.class})
public class AdminController {
	@Autowired
	AdminService adminService;
	@Autowired
	SupplierService supplierService;
	@Autowired
	CategoryService categoryService;
	@Autowired
	StockService stockService;
	@Autowired
	ArtService artService;
	@Autowired
	UserService userService;
	@Autowired
	OrderService orderService;
	@RequestMapping("/adminlist")
	public ModelAndView list() {
		ModelAndView mv= new ModelAndView();
	    List<Admin> list=adminService.list();
	    mv.addObject("list", list);
	    mv.setViewName("admin/adminlist");
		return mv;
	}
	@RequestMapping("/select")
	public ModelAndView select(String adminName) {
		ModelAndView mv = new ModelAndView();
		Admin admin=adminService.select(adminName);
		mv.addObject("Admin", admin);
		mv.setViewName("admin/adminShow");
		return mv;		
	}
	@RequestMapping("/selectByadminName")
	public ModelAndView selectByadminName(String adminName, String adminPassword,HttpSession session ) {
		ModelAndView mv1= new ModelAndView();
		  Admin admin=adminService.selectByadminName(adminName);
	     if(admin.getAdminPassword().equals(adminPassword)) {
	    	mv1.addObject("Admin", admin);
	    	mv1.setViewName("redirect:xianshi");
	     }
	     else {
	    	 mv1.addObject("error", "用户名或密码输入错误");
			 mv1.setViewName("admin/login");
	     }
		return mv1;
		
	}
	@RequestMapping("/xianshi")
	public ModelAndView xianshi(HttpSession session ){
		ModelAndView mv =new ModelAndView();
		List<Order> list2=orderService.list2();
		System.out.println(list2.size());
		session.setAttribute("a", list2.size());
		List<Order> list3=orderService.list3(1);
		session.setAttribute("d",list3.size());
		List<Order> list4=orderService.list3(2);
		session.setAttribute("e",list4.size());
		List<Order> list5=orderService.list3(3);
		session.setAttribute("f",list5.size());
		List<Order> list6=orderService.list3(4);
		session.setAttribute("h",list6.size());
		 List<User> Ulist= userService.Ulist();
	    System.out.println(Ulist.size());
		session.setAttribute("b", Ulist.size());
		 int s=stockService.select();
		 session.setAttribute("S", s);
		List<Art> Alist=artService.Alist(1);
		session.setAttribute("c", Alist.size());
		List<Art> alist=artService.Alist(2);
		session.setAttribute("g", alist.size());
		List<Category> clist=categoryService.list();
		session.setAttribute("i", clist.size());
		int j=supplierService.select();
		session.setAttribute("J", j);
		List<Admin> list=adminService.list();
		session.setAttribute("k", list.size());
		mv.setViewName("admin/index");
		return mv;
	}
	@RequestMapping("/addadmin")
	@ResponseBody
	public String add(Admin admin) {
		String date="success";
		adminService.add(admin);
		return date;	
	}
	@RequestMapping("/updateadmin")
	@ResponseBody
	public String update(Admin admin) {
		String date="success";
		System.out.println(admin.getAdminId());
		adminService.update(admin);
		return date;
	}
	@RequestMapping("/deleteadmin")
	@ResponseBody
	public String delete(int adminId) {
		
		adminService.delete(adminId);
		String date="success";
		return date;
	}
	@RequestMapping("/updatePwd")
	@ResponseBody
	public String updatePwd(HttpSession session, String adminPassword,String adminNewPassword) {
		Admin admin=(Admin) session.getAttribute("Admin");
		String date=null;
		if(admin.getAdminPassword().equals(adminPassword)) {
			admin.setAdminPassword(adminNewPassword);
			adminService.updatePwd(admin);
			 date="success";			
		}
		return date;
	}
	@RequestMapping("/adminexit")
	public ModelAndView exit(HttpSession session,SessionStatus sessionStatus) {
		
		ModelAndView mv=new ModelAndView();		
        session.invalidate(); 
        sessionStatus.setComplete();
		mv.setViewName("admin/login");
		return mv;
	}
}

